const app = require("../app");
const {
  NAME_OR_PASSWORD_IS_REQUIRED,
  NAME_IS_ALREADY_EXISTS,
  NAME_OR_PASSWORD_IS_incorrect,
  TOKEN_EXPIRED_AND_LOGIN_AGAIN,
  OPERATION_IS_NOT_ALLOW,
} = require("../config/error");

app.on("error", (err, ctx) => {
  let code = "";
  let message = "";

  switch (err) {
    case NAME_OR_PASSWORD_IS_REQUIRED:
      code = -1001;
      message = "Please enter your name or password";
      break;
    case NAME_IS_ALREADY_EXISTS:
      code = -1002;
      message = "The user name already exists";
      break;
    case NAME_OR_PASSWORD_IS_incorrect:
      code = -1003;
      message = "The user name or password is incorrect";
      break;
    case TOKEN_EXPIRED_AND_LOGIN_AGAIN:
      code = -1004;
      message = "The token has expired. Log in again";
      break;
    case OPERATION_IS_NOT_ALLOW:
      code = -1005;
      message = "The operation is not allowed";
      break;
  }
  ctx.body = { code, message };
});
